<?php

namespace mbilling\BillingBundle\Model;

use Doctrine\ORM\EntityManager;
use mbilling\BillingBundle\Entity\ordersSharedEmail;
use mbilling\BillingBundle\Entity\domains;

Class OrdersSharedEmailModel
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
    
    public function findAll()
    {
        $emails = $this->em->getRepository('BillingBundle:ordersSharedEmail')->findAll();
        return $emails;
    }
    
    public function FindByPK($email_id)
    {
        $email = $this->em->find('BillingBundle:ordersSharedEmail', $email_id);
        return $email;
    }

    public function findAllNotActive()
    {
        $emails = $this->em->getRepository('BillingBundle:ordersSharedEmail')->findBy(array('is_active' => "0"));
        return $emails;
    }
    
    public function findByDomainId($domain_id)
    {
        $emails = $this->em->getRepository('BillingBundle:ordersSharedEmail')->findBy(array('domain_id' => $domain_id));
        return $emails;
    }
    
    public function findByOrderId($order_id)
    {
        $emails = $this->em->getRepository('BillingBundle:ordersSharedEmail')->findBy(array('order_id' => $order_id));
        return $emails;
    }
    
    public function findByUserAndDomainAndOrderId($user, $domain_name, $order_id)
    {
        $domain = $this->em->getRepository('BillingBundle:domains')->findOneBy(array('name' => $domain_name));
        $email = $this->em->getRepository('BillingBundle:ordersSharedEmail')->findOneBy(array('user' => $user, 'domain_id' => $domain->getId(), 'order_id' => $order_id));
        return $email;
    }
    
    public function create($oEntity)
    {    
        $this->em->persist($oEntity);
        $this->em->flush();
    }
    
    public function removeByPK($email_id)
    {
        $email = $this->em->find('BillingBundle:ordersSharedEmail', $email_id);
        $this->em->remove($email);
        $this->em->flush();
    }
}
?>
